package com.cy.aircompressor.main.home.other;

import android.os.Bundle;

import androidx.fragment.app.FragmentManager;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.cy.aircompressor.config.AirConfig;
import com.cy.aircompressor.databinding.DialogWarningLayoutBinding;
import com.cy.aircompressor.entity.select.SelectDict;
import com.cy.aircompressor.entity.warning.WarningMsg;
import com.cy.aircompressor.main.home.other.provider.WarningProvider;
import com.cy.aircompressor.main.ui.other.viewmodel.OtherViewModel;
import com.cy.aircompressor.main.view.PageTurningIndicator;
import com.cy.aircompressor.main.view.pop.AirDropdownPopupWindow;
import com.drug.lib.util.DensityUtil;
import com.drug.lib.util.ToastUtil;
import com.drug.lib.view.recycptr.DynamicAdpTypePool;
import com.drug.lib.view.recycptr.DynamicRecyclerAdapter;
import com.drug.lib.view.recycptr.bean.RowEntity;
import com.jakewharton.rxbinding2.view.RxView;
import com.net.model.base.Resource;
import com.net.model.base.fragment.BaseVMDialogFragment;
import com.net.model.utils.Constant;

import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

public class WarningFragment extends BaseVMDialogFragment<DialogWarningLayoutBinding, OtherViewModel> {
    private Integer pageNum = 1;
    private Integer pageSize = 10;
    private Integer pageTotal = 1;
    private String compressorId;

    public static void showDialogFragment(FragmentManager fragmentManager, String tag, String compressorId) {
        Bundle args = new Bundle();
        WarningFragment fragment = new WarningFragment();
        args.putString("compressorId", compressorId);
        fragment.setArguments(args);
        fragment.show(fragmentManager, tag);
    }

    @Override
    protected DialogWarningLayoutBinding getViewBinding() {
        return DialogWarningLayoutBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void initData() {
        compressorId = getArguments().getString("compressorId");
        initView();
        initSensorDataRecycler();
    }

    @Override
    protected void requestNetwork() {
        mViewModel.queryWarningByPage(pageNum, pageSize, compressorId);
    }


    @Override
    protected int getWindowHeight() {
        return (getScreenHeight() - DensityUtil.dip2px(78));
    }

    @Override
    protected int getWindowWidth() {
        return (super.getScreenWidth() - DensityUtil.dip2px(106));
    }

    @Override
    protected void dataChange() {
        mViewModel.getQueryWarningByPageObservable().observe(this, new Observer<Resource<RowEntity<WarningMsg>>>() {
            @Override
            public void onChanged(Resource<RowEntity<WarningMsg>> rowEntityResource) {
                rowEntityResource.handler(new OnCallback<RowEntity<WarningMsg>>() {
                    @Override
                    public void onSuccess(RowEntity<WarningMsg> data) {
                        int remainder = data.getTotal() % pageSize;
                        int page = data.getTotal() / pageSize;
                        pageTotal = remainder > 0 ? (page + 1) : page;
                        mBinding.nodata.dismiss();
                        mBinding.indicator.setRecordTotal(data.getTotal());
                        dynamicRecyclerAdapter.setItems(data.getList());
                        dynamicRecyclerAdapter.notifyDataSetChanged();
                    }

                    @Override
                    public void onFailure(Integer code, String message) {
                        super.onFailure(code, message);
                        if (code == Constant.EMPTY_DATA_ERROR) {
                            dynamicRecyclerAdapter.setItems(new ArrayList<>());
                            dynamicRecyclerAdapter.notifyDataSetChanged();
                            mBinding.nodata.show();
                        }
                    }
                });
            }
        });

        mViewModel.getUpdateStatus().observe(this, new Observer<Resource<Object>>() {
            @Override
            public void onChanged(Resource<Object> objectResource) {
                objectResource.handler(new OnCallback<Object>() {
                    @Override
                    public void onSuccess(Object data) {
                        ToastUtil.show("处理成功");
                        requestNetwork();
                    }
                });
            }
        });
    }


    DynamicRecyclerAdapter dynamicRecyclerAdapter;

    private void initSensorDataRecycler() {
        LinearLayoutManager mLayoutManager = new LinearLayoutManager(mContext);
        mBinding.recyclerView.setLayoutManager(mLayoutManager);
        dynamicRecyclerAdapter = new DynamicRecyclerAdapter();
        DynamicAdpTypePool pool = new DynamicAdpTypePool();
        WarningProvider warningProvider = new WarningProvider(getChildFragmentManager(), mContext);
        pool.register(WarningMsg.class, warningProvider);
        dynamicRecyclerAdapter.registerAll(pool);
        mBinding.recyclerView.setAdapter(dynamicRecyclerAdapter);
        warningProvider.setHandleCallBack(new WarningProvider.HandleCallBack() {
            @Override
            public void handle(WarningMsg warningMsg) {
                mViewModel.updateStatus(warningMsg.getId());
            }
        });
    }


    private void initView() {
        mBinding.indicator.setPageSize(pageSize);
        mBinding.indicator.setPageNum(pageNum);
        mBinding.indicator.setPageTurningIndicatorCallBack(new PageTurningIndicator.PageTurningIndicatorCallBack() {
            @Override
            public void skipPage(int pageNum) {
                WarningFragment.this.pageNum = pageNum;
                requestNetwork();
            }

            @Override
            public void pageSize(int size) {
                WarningFragment.this.pageSize = size;
                requestNetwork();
            }
        });
        RxView.clicks(mBinding.selectedAir)
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(o -> {
                    AirDropdownPopupWindow mPopupWindow = new AirDropdownPopupWindow(mContext, mBinding.selectedAir, mBinding.selectedAir, AirConfig.get().compressors);
                    mPopupWindow.setDropdownCallBack(new AirDropdownPopupWindow.DropdownCallBack() {
                        @Override
                        public void select(SelectDict selectDict) {
                            WarningFragment.this.compressorId = selectDict.getValue();
                            requestNetwork();
                        }
                    });
                    mPopupWindow.showAsDropDown(mBinding.selectedAir);//显示PopupWindow小弹窗
                });
        RxView.clicks(mBinding.alarmThreshold)
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(o -> {
                    AlarmThresholdFragment.showDialogFragment(getChildFragmentManager(), "1");
                });
        RxView.clicks(mBinding.closeBtn)
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(o -> {
                    dismiss();
                });
    }
}
